﻿using Ken_WPF_UI.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Ken_WPF_UI.View
{
    /// <summary>
    /// MainFrameWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainFrameWindow : Window
    {
        public MainFrameWindow()
        {
            InitializeComponent();
            
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
        }


        /// <summary>
        /// 创造grid的内容
        /// </summary>
        /// <param name="collums">展示列内容</param>
        /// <param name="rowCount">每行最大的列数</param>
        /// <param name="rowTotal">总行数</param>
        private void CreateGridContent(string[] collums,int rowCount, int rowTotal)
        {
           

            rowTotal = collums.Length % rowCount == 0 ? collums.Length / rowCount : collums.Length / rowCount + 1;

            if (this.mainContent == null)
            {
                return;
            }
            
            this.mainContent.Children.Clear();
            this.mainContent.RowDefinitions.Clear();
            this.mainContent.ColumnDefinitions.Clear();

            //int totalRecord = 0;

            //行
            for (int r = 0; r < rowTotal; r++)
            {
                this.mainContent.RowDefinitions.Add(new RowDefinition());
            }

            //列
            for (int i = 0; i < rowCount; i++)
            {
                this.mainContent.ColumnDefinitions.Add(new ColumnDefinition());
            }


            List<RowColums> list = new List<RowColums>();
            RowColums rowColums;

            //行与列 组合
            //行
            for (int r = 0; r < rowTotal; r++)
            {
                //列
                for (int i = 0; i < rowCount; i++)
                {
                    rowColums = new RowColums() { RowIndex = r, ColumIndex = i };
                    list.Add(rowColums);
                }
            }

            Label label;
            Border border;
            string[] splits;
            string content;
            for (int i = 0; i < collums.Length; i++)
            {
                splits = Regex.Split(collums[i].Replace("Window", "控件"),"\\.");
                content = splits[splits.Length-1];
                border = new Border() { CornerRadius = new CornerRadius(10), Background = new SolidColorBrush(Colors.YellowGreen), BorderThickness = new Thickness(1) };
                label = new Label() { Name = Regex.Replace(collums[i], "\\.", "_"), Content = content };
                label.HorizontalAlignment = HorizontalAlignment.Center;
                label.VerticalAlignment = VerticalAlignment.Center;
                label.Foreground = new SolidColorBrush(Colors.White);
                //label.FontWeight = FontWeights.Bold;
                label.FontSize = 12;
                label.MouseDown += Label_MouseDown;
                label.Padding= new Thickness(20,20,20,20);


                border.Child = label;

                if (i== collums.Length - 1)
                {
                    Grid.SetColumnSpan(border, list.Count - collums.Length+1);
                }
                
                Grid.SetRow(border, list[i].RowIndex);
                Grid.SetColumn(border, list[i].ColumIndex);
                this.mainContent.Children.Add(border);
            }


        }

        /// <summary>
        /// 鼠标按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Label_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if(sender is Label)
            {
                Type type = Type.GetType("Ken_WPF_UI.View." +(sender as Label).Name.Replace('_','.'));
                object obj = Activator.CreateInstance(type, true);//根据类型创建实例
                if(obj is Window)
                {
                  (obj as Window).Owner = this;

                   (obj as Window).WindowStartupLocation = WindowStartupLocation.CenterOwner;
                   (obj as Window).Width = (obj as Window).Width;
                    (obj as Window).Height = (obj as Window).Height;
                   (obj as Window).ShowDialog();
                }
            }
            
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            (this.listMenu.Items[0] as ListViewItem).IsSelected = true;
        }

        /// <summary>
        /// 选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListViewItem_Selected(object sender, RoutedEventArgs e)
        {
            Console.WriteLine(
            (this.listMenu.Items[1] as ListViewItem).Background.ToString());
            ListBoxItem listBoxItem = sender as ListBoxItem;
            object tag = listBoxItem.Tag;
            string[] collums;
            int rowCount = 4;
            int rowTotal = 0;
            if (tag != null)
            {

                switch (tag.ToString())
                {
                    case "buju":
                        collums = new string[] { "StackPanelWindow", "WrapPanelWindow", "DockPanelWindow", "GridWindow", "CanvasWindow" };
                        CreateGridContent(collums, rowCount, rowTotal);
                        break;
                    case "base":
                        collums = new string[] { "BorderWindow", "CalendarWindow", "DatePickerWindow", "ImageWindow", "SliderWindow", "ProgressBarWindow", "GroupBoxWindow" , "ExpanderWindow" , "FrameWindow", "CheckBoxWindow", "MenuWindow", "ContextMenuWindow" };
                        CreateGridContent(collums, rowCount, rowTotal);
                        break;
                    case "items":
                        collums = new string[] {"ComboBoxWindow", "TabControlWindow", "ListBoxWindow" };
                        CreateGridContent(collums, rowCount, rowTotal);
                        break;
                    case "view":
                        collums = new string[] { "ListViewWindow" , "DataGridWindow" , "TreeViewWindow", "ToolBarWindow" ,"StatusBarWindow", "MediaElementWindow", "RichTextBoxWindow" };
                        CreateGridContent(collums, rowCount, rowTotal);
                        break;
                    case "otherUI":
                        collums = new string[] { "otherUI.Login01Window", "MediaElementWindow2", "otherUI.SystemManageWindow", "otherUI.RegisterWindow1", "otherUI.MenuListWindow", "otherUI.WechatWindow", "otherUI.WechatUserSetWindow" };
                        CreateGridContent(collums, rowCount, rowTotal);
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
